var app = angular.module("myApp",['ngRoute']);
//路由器
app.config(function($routeProvider){
    $routeProvider.
    when("/home", {
      controller: 'home',
      templateUrl: 'view/home.html',
    }).
    when("/signin",{ // 手表信息管理页面
       controller: 'signin',
       templateUrl: 'view/signin.html',
    }).
    when("/signup",{   // 用户信息管理
       controller: 'signup',
       templateUrl: 'view/signup.html',
    }).
    when("/signout",{  //用户意见反馈
       controller: 'signout',
       templateUrl: 'view/signout.html',
    }).
    otherwise({
       redirectTo:'/home'
    });
});
// //监听路由
﻿app.run(function($rootScope) {
    $rootScope.$on('$routeChangeStart', function(evt, next, current) {
      
    });
});
app.controller("app", function ($scope, $rootScope){
	$scope.testback = function() {
		window.history.back(-1);
	}
})
app.controller("home", function ($scope, $rootScope,serve){
	$rootScope.title = "首页"
  $scope.testToken = function() {
    serve.get("/api/signin")
              .then(function (user) {
                layer.alert("没有过期!" + user.name);
              })
  }
})
app.controller("signin", function ($scope, $rootScope, serve, $http) {
	$rootScope.title = "登录"
  $scope.login = function() {
    var user = {
      name : $scope.inputName,
      password : $scope.inputPassword
    }
    $http.post("/api/signin",user)
          .then(function (data) {
            // layer.alert(data)
            console.log(data.data);
            $rootScope.token = data.data;
            serve.get("/api/signin")
              .then(function (user) {
                $rootScope.user = user;
                window.location.href = '/#/home';
              })
          }, function (err){
            layer.alert("出错了");
          })
  }
})

app.controller("signup", function ($scope, $rootScope, serve, $http){
	$rootScope.title = "注册";
  $scope.logup = function() {
    var user = {
      name : $scope.inputName,
      password : $scope.inputPassword
    }
    $http.post("/api/signup",user)
          .then(function (data) {
            layer.alert(data)
          }, function (err){
            layer.alert("出错了");
          })
  }
})
app.controller("signout", function ($scope, $rootScope, serve){
	$rootScope.title = "登出";
  $scope.logout = function() {
    serve.get("/api/signout")
          .then(function (data) {
            layer.alert(data)
          })
  }
})
app.service("serve",function ($http, $q, $rootScope) {
  this.get = function(urlStr) {
      var deferred = $q.defer();
      if($rootScope.token) {
        var reg = /\?/;
        if(reg.test(urlStr)) {
          urlStr = urlStr + "&&token=" + $rootScope.token;
        } else {
          urlStr = urlStr + "?token=" + $rootScope.token;
        }
      }
      $http.get(urlStr).  
          success(function(data, status, headers, config) { 
            deferred.resolve(data);   
          }).error(function(data, status, headers, config) {
            if (!!data.describe) {
              return layer.alert(data.describe)
            }
            layer.alert("出错了");
            deferred.reject(data);   
          });  
      return deferred.promise; 
  }
  this.post = function(url,data) {
      var deferred = $q.defer(); 
      $http.post(url,data)
        .success(function(data, status, headers, config) { 
          deferred.resolve(data);   
        }).error(function(data, status, headers, config) {
          if (!!data.describe) {
              return layer.alert(data.describe)
          }
          layer.alert("出错了");
          deferred.reject(data);   
        }); 
      return deferred.promise;
  }
})